package edu.cmu.arch.pipesandfilters;

import edu.cmu.arch.pipesandfilters.frame.Variable;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
* File:SystemA.java
* Course: 17655
* Project: Assignment 1
* Group: Group 1
* @author Michael Azzopardi
* Versions:
*	1.0 February 2012
*
* This is the main program for the implementation of the Pipe-and-Filter network for System A
* 
*/
public class SystemA {
	/** 
	 * To run the system pass the input filename and output filename as parameters.
	 * java SystemA.java [inputfilename.dat] [outputfilename.dat]
	 * 
	 * @param args
	 * @author Michael Azzopardi
	 */
	public static void main(String[] args) {
		System.out.println("Initializing System A Pipe-and-Filter Network");
		System.out.println("developed by Group 1");
		System.out.println("=============================================");
		
		// Instantiate fileSinkFilter
		try {	
			// Define file names default - replace filenames with parameters
			String sourceFilename = "DataSets" + File.separator + "FlightData.dat";
			String outputFilename = "DataSets" + File.separator + "OutputA.dat";
			
			if (args.length > 0) {
				if (args[1] != null) {
					sourceFilename = args[0];
				} else {
					System.out.println("Missing input filename parameter");
				}
				if (args[1] != null) {
					outputFilename = args[1];
				} else {
					System.out.println("Missing output filename parameter");
				}
			}
			System.out.println("Starting System A with input "+sourceFilename+" and output "+outputFilename);

			// DefineFilters
			SourceFilter sourceFilter;
			FileSinkFilter sinkFilter;
			ConversionFilter tempConversionFilter;
			ConversionFilter altConversionFilter;
			StripFilter stripFilter;
			ArrayList<Variable> stripMeasurements = new ArrayList<Variable>();
			
			// Instantiate sourceFilter
			sourceFilter = new SourceFilter(sourceFilename);
			// Initialize StripFilter
			// set up strip filters 
			stripMeasurements.add(Variable.Time);
			stripMeasurements.add(Variable.Temperature);
			stripMeasurements.add(Variable.Altitude);
			stripFilter = new StripFilter(stripMeasurements);
			
			// Initialize and set fileName and separator for sinkFilter
			sinkFilter = new FileSinkFilter( stripMeasurements,outputFilename);

			// Instantiate filters
			tempConversionFilter = new ConversionFilter(Variable.Temperature);
			altConversionFilter = new ConversionFilter(Variable.Altitude);			

			// Connect filters			
			stripFilter.Connect(sourceFilter);			
			tempConversionFilter.Connect(stripFilter);
			altConversionFilter.Connect(tempConversionFilter);
			sinkFilter.Connect(altConversionFilter);						
			
			// Start Filters
			sourceFilter.start();
			stripFilter.start();
			tempConversionFilter.start();
			altConversionFilter.start();
			sinkFilter.start();						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Error : "+e.getMessage());
			e.printStackTrace();
		}				
	}

}

		
